package com.spzx.product.controller;

import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.common.log.annotation.Log;
import com.spzx.common.log.enums.BusinessType;
import com.spzx.common.security.annotation.RequiresPermissions;
import com.spzx.product.domain.ProductUnit;
import com.spzx.product.service.ProductUnitService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "商品单位接口管理")
@RestController
@RequestMapping("/productUnit")
public class ProductUnitController extends BaseController {

    @Autowired
    ProductUnitService productUnitService;

    @RequiresPermissions("base:productUnit:list")
    @Operation(summary = "获取商品单位分页列表")
    @GetMapping("/list")
    public TableDataInfo list(ProductUnit productUnit) {
        startPage();
        List<ProductUnit> list = productUnitService.selectList(productUnit);
        return getDataTable(list);
    }

    @RequiresPermissions("base:productUnit:query")
    @Operation(summary = "获取商品单位详细信息")
    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        ProductUnit productUnit = productUnitService.getInfo(id);
        return success(productUnit);
    }

    @Log(title = "商品单位", businessType = BusinessType.INSERT)
    @RequiresPermissions("base:productUnit:add")
    @Operation(summary = "新增商品单位")
    @PostMapping
    public AjaxResult save(@RequestBody @Validated ProductUnit productUnit) {
        productUnitService.save(productUnit);
        return success();
    }

    @Log(title = "商品单位", businessType = BusinessType.UPDATE)
    @RequiresPermissions("base:productUnit:edit")
    @Operation(summary = "修改商品单位")
    @PutMapping
    public AjaxResult update(@RequestBody @Validated ProductUnit productUnit) {
        productUnitService.update(productUnit);
        return success();
    }

    @Log(title = "商品单位", businessType = BusinessType.DELETE)
    @RequiresPermissions("base:productUnit:remove")
    @Operation(summary = "删除商品单位")
    @DeleteMapping("/{ids}")
    public AjaxResult delete(@PathVariable("ids") Long[] ids) {
        productUnitService.delete(ids);
        return success();
    }

    @RequiresPermissions("base:productUnit:query")
    @Operation(summary = "获取全部单位")
    @GetMapping("/getUnitAll")
    public AjaxResult selectProductUnitAll() {
        List<ProductUnit> list = productUnitService.selectProductUnitAll();
        return success(list);
    }
}
